﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Annotations;
using System.Windows.Documents;
using System.Windows.Input;
using WFP_Sandbox.Model;
using WFP_Sandbox.ViewModel.UI_Classes;

namespace WFP_Sandbox.ViewModel.Helper
{
    public static class ConvertModelData
    {

        internal static List<Game> MapGameValues(List<GameValue> list)
        {
            if (list == null )
                return null;
            var tmpGames = new List<Game>(list.Select(MapGameValueToGame).ToList().Distinct(new GameComparer(m=>m.GameNumber)));
            return tmpGames;
        }

        private static Game MapGameValueToGame(GameValue m_GameValue)
        {
            if (m_GameValue == null)
                return null;

            var tmpGame = new Game
            {
                GameNumber = m_GameValue.GameNumber,
                GameDate = m_GameValue.GameDate
            };
            return tmpGame;
            
            
        }
        
        internal static List<PlayerResult> MapPlayerResultValues(List<GameValue> list)
        {
            if (!list.Any())
                return null;
            var tmpPlayers = new List<Player>(list.Select(MapGameValueToPlayer).ToList());

            return (from tmpPlayer in tmpPlayers.GroupBy(t => new {t.Sport, t.Name, t.TeeNumber}).Select(p => new {p.Key.Sport, p.Key.Name, p.Key.TeeNumber})
                    let tmpGameResultsPerPlayer = list.Where(t => (t.Player == tmpPlayer.Name) && (t.Sport == tmpPlayer.Sport)).Select(MapGameValueToGameResult).OrderBy(d => d.GameDate).ToList()
                    select new PlayerResult(tmpGameResultsPerPlayer, new Player { Sport = tmpPlayer.Sport, Name = tmpPlayer.Name, TeeNumber = tmpPlayer.TeeNumber })).ToList();
        }

        private static Player MapGameValueToPlayer(GameValue m_GameValue)
        {
            if(m_GameValue == null)
                return null;
            var tmpPlayer = new Player
            {
                Sport = m_GameValue.Sport,
                Name = m_GameValue.Player,
                TeeNumber = m_GameValue.TeeNumber

            };
            return tmpPlayer;
        }

        private static GameResult MapGameValueToGameResult(GameValue m_GameValue)
        {
            if (m_GameValue == null)
                return null;
            var tmpGameResult = new GameResult
            {
                GameDate = m_GameValue.GameDate,
                GameNumber = m_GameValue.GameNumber,
                Result = m_GameValue.GameResult

            };
            return tmpGameResult;
        }


        internal static IEnumerable<string> MapSports(List<GameValue> list)
        {
            if(list == null)
                return null;

            var tmpTrainers = new List<string>(list.Select(p => p.Sport).ToList().Distinct());
            return tmpTrainers;
        }
    }
}
